export default {
  methods: {
    genParam2() {
      const { selected, item, operateParam_, idField } = this
      const { param = {}, reqRow } = operateParam_
      const urlParam = {}
      urlParam.pageType = item.type
      const selectedRow = selected[0] || {}
      delete selectedRow.mnplt_tpcd
      Object.keys(param).forEach(key => {
        const paramValue = param[key]
        if (paramValue && !key.includes('grp')) {
          urlParam[key] = paramValue
          return true
        }
        if (key.includes('grp')) {
          this.grpKey = key
          const keyType = typeof paramValue
          if (keyType === 'string') {
            const fields = paramValue.split(',')
            urlParam[key] = selected.reduce((pre, next) => {
              const data = {}
              for (const field of fields) {
                data[field] = next[field]
              }
              pre.push(data)
              return pre
            }, [])
          } else {
            urlParam[key] = selected
          }
        } else {
          urlParam[key] = selectedRow[key]
        }
      })
      const idValue = selectedRow[idField]
      if (idValue) {
        urlParam[idField] = idValue
      }
      if (reqRow) {
        Object.assign(urlParam, selectedRow)
      }
      return urlParam
    },
    canOpen() {
      const { selected, item } = this
      const { selectType } = item
      return this.isSelected(selected, selectType)
    },
    validate() {
      const { selected, operateParam_ } = this
      const { isValid, validate } = operateParam_
      return !isValid || this.canOper(selected, validate)
    },
    operate() {
      // 判断单选多选
      const { operateParam_: { transId, content, fn }, item: { name }} = this
      if (!this.canOpen() || !this.validate()) {
        return
      }
      const param = this.genParam()
      /* if (selected.length === tableData.length && page_jump !== 1) {
        pagination.page_jump -= 1
      }*/
      console.log('请求参数为：', param)
      this.$sure(content || `确认${name}？`, () => {
        this.$emit('showLoading', true)
        this.post(transId, param, { showLoading: false }).then(ret => {
          this.$emit('showLoading', false)
          if (fn) {
            fn.call(this, ret)
          } else {
            this.$info(name + '成功！', () => {
              this.$emit('reqData')
            })
          }
        }).catch(_ => {
          this.$emit('showLoading', false)
        })
      })

      /* this.$confirm(content || `确认${item.name}？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const { transId, isValid, validate, content, fn } = operateParam_
        this.post(transId, param,)
        this.$message({
          message: '操作成功',
          type: 'success'
        })
      }).catch(() => {
      })*/
    }
  }
}
